package com.example.testdemo.demos.web.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.testdemo.demos.web.Config.JwtInfo;
import com.example.testdemo.demos.web.Config.Result;
import com.example.testdemo.demos.web.Config.ResultEnum;
import com.example.testdemo.demos.web.Dao.DictDao;
import com.example.testdemo.demos.web.Dao.DictItemDao;
import com.example.testdemo.demos.web.Entity.Dict;
import com.example.testdemo.demos.web.Entity.DictItem;
import com.example.testdemo.demos.web.Entity.MenuItem;
import com.example.testdemo.demos.web.Service.DictService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.example.testdemo.demos.web.Tool.WrapperMethodUtil.listBool;

@Service
public class DictServiceImpl implements DictService {

    @Resource
    private DictDao dictDao;

    @Resource
    private DictItemDao dictItemDao;


    /**
     * 查询字典接口
     * @param dict
     * @return
     */
    @Override
    public Result queryDict(Dict dict, JwtInfo jwtInfo){
        Result result =new Result();
        try {
            LambdaQueryWrapper<Dict> lqwDict =new LambdaQueryWrapper<>();
            lqwDict.eq(Dict::getId,dict.getId());
            lqwDict.eq(Dict::getStatus,1);
            Dict dict1 =dictDao.selectOne(lqwDict);
            List<DictItem> list =new ArrayList<>();
            if (dict1.getPermissions().equals("ordinary")){
                LambdaQueryWrapper<DictItem> lqw =new LambdaQueryWrapper<>();
                lqw.eq(DictItem::getParentId,dict1.getId());
                lqw.eq(DictItem::getStatus,1);
                list =dictItemDao.selectList(lqw);
            }else {
                for (String item2:jwtInfo.getPurview()){
                    if (item2.contains(dict1.getPermissions())){
                        LambdaQueryWrapper<DictItem> lqw =new LambdaQueryWrapper<>();
                        lqw.eq(DictItem::getParentId,dict1.getId());
                        lqw.eq(DictItem::getStatus,1);
                        list =dictItemDao.selectList(lqw);
                        break;
                    }
                }
            }
            if (list.isEmpty()){
                result.setCode(ResultEnum.FAILED_VISA_INSPECTION.getCode());
                result.setMsg(ResultEnum.FAILED_VISA_INSPECTION.getData());
            }else {
                result.setCode(ResultEnum.SUCCESS.getCode());
                result.setMsg(dict1.getName()+"查询成功");
                result.setData(list);
            }
        }catch (Exception e){
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg(ResultEnum.UNKNOWNERROR.getData());
            result.setData(e);
        }
        return  result;
    }

}
